package com.clkj.cldisabledmanclient.mvp.recovertrainapply;

import android.Manifest;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.base.BaseActivity;
import com.clkj.cldisabledmanclient.util.MyGlideEngine;
import com.clkj.cldisabledmanclient.util.ToastUtil;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.io.File;
import java.util.List;

import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

//添加康复训练申请的图片
public class AddKfxlApplyFileActivity extends BaseActivity implements View.OnClickListener {

    private ImageView mIvGrzp;
    private ImageView mIvJhrsfz;
    private ImageView mIvJhrhkb;
    private ImageView mIvEthkb;
    private ImageView mIvDbzhpkzm;
    private ImageView mIvCjz;
    private ImageView mIvYyzm;
    private TextView mTvEnsure;

    private int REQUST_GRZP = 0x11;
    private int REQUST_JHRSFZ = 0x12;
    private int REQUST_JHRHKB = 0x13;
    private int REQUST_ETHKB = 0x14;
    private int REQUST_DBZHPKZM = 0x15;
    private int REQUST_CJZ = 0x16;
    private int REQUST_YYZM = 0x17;

    private String grzpnewPath;
    private String jhrsfznewPath;
    private String jhrhkbnewPath;
    private String ethkbnewPath;
    private String dbzhpkzmnewPath;
    private String cjznewPath;
    private String yyzmnewPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_kfxl_apply_file);
        initTitle(null, null, "添加图片", true, null);
        initData();
        initView();
        assignView();

        requestGetPicPermission();
    }

    @Override
    public void initData() {

        Intent data = getIntent();
        grzpnewPath = data.getStringExtra("grzpnewPath");
        jhrsfznewPath = data.getStringExtra("jhrsfznewPath");
        jhrhkbnewPath = data.getStringExtra("jhrhkbnewPath");
        ethkbnewPath = data.getStringExtra("ethkbnewPath");
        dbzhpkzmnewPath = data.getStringExtra("dbzhpkzmnewPath");
        cjznewPath = data.getStringExtra("cjznewPath");
        yyzmnewPath = data.getStringExtra("yyzmnewPath");
    }

    @Override
    public void assignView() {
        RequestOptions requestOptions = new RequestOptions().centerCrop().placeholder(R.mipmap.icon_up_photo)
                .error(R.mipmap.icon_up_photo).centerCrop();
        if (!TextUtils.isEmpty(grzpnewPath)) {
            Glide.with(AddKfxlApplyFileActivity.this).load(grzpnewPath).apply(requestOptions)
                    .into(mIvGrzp);
        }
        if (!TextUtils.isEmpty(jhrsfznewPath)) {
            Glide.with(AddKfxlApplyFileActivity.this).load(jhrsfznewPath).apply(requestOptions)
                    .into(mIvJhrsfz);
        }

        if (!TextUtils.isEmpty(jhrhkbnewPath)) {
            Glide.with(AddKfxlApplyFileActivity.this).load(jhrhkbnewPath).apply(requestOptions)
                    .into(mIvJhrhkb);
        }


        if (!TextUtils.isEmpty(ethkbnewPath)) {
            Glide.with(AddKfxlApplyFileActivity.this).load(ethkbnewPath).apply(requestOptions)
                    .into(mIvEthkb);
        }

        if (!TextUtils.isEmpty(dbzhpkzmnewPath)) {
            Glide.with(AddKfxlApplyFileActivity.this).load(dbzhpkzmnewPath).apply(requestOptions)
                    .into(mIvDbzhpkzm);
        }

        if (!TextUtils.isEmpty(cjznewPath)) {
            Glide.with(AddKfxlApplyFileActivity.this).load(cjznewPath).apply(requestOptions)
                    .into(mIvCjz);
        }

        if (!TextUtils.isEmpty(yyzmnewPath)) {
            Glide.with(AddKfxlApplyFileActivity.this).load(yyzmnewPath).apply(requestOptions)
                    .into(mIvYyzm);
        }

        mIvGrzp.setOnClickListener(this);
        mIvJhrsfz.setOnClickListener(this);
        mIvJhrhkb.setOnClickListener(this);
        mIvEthkb.setOnClickListener(this);
        mIvDbzhpkzm.setOnClickListener(this);
        mIvCjz.setOnClickListener(this);
        mIvYyzm.setOnClickListener(this);
        mTvEnsure.setOnClickListener(this);
    }

    @Override
    public void initView() {

        mIvGrzp = findViewById(R.id.iv_grzp);
        mIvJhrsfz = findViewById(R.id.iv_jhrsfz);
        mIvJhrhkb = findViewById(R.id.iv_jhrhkb);
        mIvEthkb = findViewById(R.id.iv_ethkb);
        mIvDbzhpkzm = findViewById(R.id.iv_dbzhpkzm);
        mIvCjz = findViewById(R.id.iv_cjz);
        mIvYyzm = findViewById(R.id.iv_yyzm);
        mTvEnsure = findViewById(R.id.tv_ensure);
    }

    private void requestGetPicPermission() {
        Dexter.withActivity(this)
                .withPermissions(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .withListener(new MultiplePermissionsListener() {
                    @Override
                    public void onPermissionsChecked(MultiplePermissionsReport report) {
                        /* ... */
                        if (report.areAllPermissionsGranted()) {

                        } else {

                            ToastUtil.showShort(AddKfxlApplyFileActivity.this, "我们需要获取相关权限保证App的正常运行，请去设置界面进行相关设置");
                        }
                    }

                    @Override
                    public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
                        /* ... */
                        token.continuePermissionRequest();
                    }
                }).check();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            //个人照片
            case R.id.iv_grzp:
                chooseImg(REQUST_GRZP);
                break;
            //监护人身份证
            case R.id.iv_jhrsfz:
                chooseImg(REQUST_JHRSFZ);
                break;
            //监护人户口簿
            case R.id.iv_jhrhkb:
                chooseImg(REQUST_JHRHKB);
                break;
            //儿童户口簿
            case R.id.iv_ethkb:
                chooseImg(REQUST_ETHKB);
                break;
            //低保证和贫困证明
            case R.id.iv_dbzhpkzm:
                chooseImg(REQUST_DBZHPKZM);
                break;
            //残疾证
            case R.id.iv_cjz:
                chooseImg(REQUST_CJZ);
                break;
            //医院证明
            case R.id.iv_yyzm:
                chooseImg(REQUST_YYZM);
                break;

            //确定提交
            case R.id.tv_ensure:

                if (TextUtils.isEmpty(grzpnewPath)) {
                    ToastUtil.showShort(AddKfxlApplyFileActivity.this, "请上传个人照片");
                    return;
                }
                if (TextUtils.isEmpty(jhrsfznewPath)) {
                    ToastUtil.showShort(AddKfxlApplyFileActivity.this, "请上传监护人身份证照片");
                    return;
                }
                if (TextUtils.isEmpty(jhrhkbnewPath)) {
                    ToastUtil.showShort(AddKfxlApplyFileActivity.this, "请上传监护人户口簿照片");
                    return;
                }
                if (TextUtils.isEmpty(ethkbnewPath)) {
                    ToastUtil.showShort(AddKfxlApplyFileActivity.this, "请上传儿童户口簿照片");
                    return;
                }
                /*if (TextUtils.isEmpty(dbzhpkzmnewPath)) {
                    ToastUtil.showShort(AddKfxlApplyFileActivity.this, "请上传低保证或贫困证明");
                    return;
                }*/

               /* if (TextUtils.isEmpty(cjznewPath)) {
                    ToastUtil.showShort(AddKfxlApplyFileActivity.this, "请上传残疾证照片");
                    return;
                }*/
                if (TextUtils.isEmpty(yyzmnewPath)) {
                    ToastUtil.showShort(AddKfxlApplyFileActivity.this, "请上传医院证明");
                    return;
                }

                Intent toApply = new Intent();
                toApply.putExtra("grzpnewPath", grzpnewPath);
                toApply.putExtra("jhrsfznewPath", jhrsfznewPath);
                toApply.putExtra("jhrhkbnewPath", jhrhkbnewPath);
                toApply.putExtra("ethkbnewPath", ethkbnewPath);
                toApply.putExtra("dbzhpkzmnewPath", dbzhpkzmnewPath);
                toApply.putExtra("cjznewPath", cjznewPath);
                toApply.putExtra("yyzmnewPath", yyzmnewPath);
                setResult(RESULT_OK, toApply);
                finish();
                break;

        }
    }

    private void chooseImg(int requesttype) {

        Matisse.from(this)
                .choose(MimeType.ofImage())
                .countable(true)
                .capture(true)
                .captureStrategy(
                        new CaptureStrategy(true, "com.clkj.cldisabledmanclient.fileprovider"))
                .maxSelectable(1)
                .gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
                .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                .thumbnailScale(0.60f)
                .imageEngine(new MyGlideEngine())
                .forResult(requesttype);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //mSelectedImgAttachment = Matisse.obtainResult(data);
        if (resultCode == RESULT_OK) {
            //假如是个人照片
            if (requestCode == REQUST_GRZP) {
                Uri uri = Matisse.obtainResult(data).get(0);
                String grzpPath = Matisse.obtainPathResult(data).get(0);
                lubanImage("grzp", grzpPath);
            }

            //假如是监护人身份证
            if (requestCode == REQUST_JHRSFZ) {
                Uri uri = Matisse.obtainResult(data).get(0);
                String jhrsfzPath = Matisse.obtainPathResult(data).get(0);
                lubanImage("jhrsfz", jhrsfzPath);
            }

            //假如是监护人户口簿
            if (requestCode == REQUST_JHRHKB) {
                Uri uri = Matisse.obtainResult(data).get(0);
                String jhrhkbPath = Matisse.obtainPathResult(data).get(0);
                lubanImage("jhrhkb", jhrhkbPath);
            }

            //假如儿童户口簿
            if (requestCode == REQUST_ETHKB) {
                Uri uri = Matisse.obtainResult(data).get(0);
                String ethkbPath = Matisse.obtainPathResult(data).get(0);
                lubanImage("ethkb", ethkbPath);
            }

            //假如是低保证或贫困证明
            if (requestCode == REQUST_DBZHPKZM) {
                Uri uri = Matisse.obtainResult(data).get(0);
                String dbzhpkzmPath = Matisse.obtainPathResult(data).get(0);
                lubanImage("dbzhpkzm", dbzhpkzmPath);
            }

            //假如是残疾证
            if (requestCode == REQUST_CJZ) {
                Uri uri = Matisse.obtainResult(data).get(0);
                String cjzPath = Matisse.obtainPathResult(data).get(0);
                lubanImage("cjz", cjzPath);
            }
            //假如是医院证明
            if (requestCode == REQUST_YYZM) {
                Uri uri = Matisse.obtainResult(data).get(0);
                String yyzmPath = Matisse.obtainPathResult(data).get(0);
                lubanImage("yyzm", yyzmPath);
            }
        }
    }

    //鲁班压缩一张图片
    private void lubanImage(String type, String imagePath) {

        //在这里做图片压缩
        File oldFile = new File(imagePath);

        Luban.with(this) // 初始化
                .load(oldFile) // 要压缩的图片
                .putGear(4)
                .setCompressListener(new OnCompressListener() {
                    @Override
                    public void onStart() {
                    }

                    @Override
                    public void onSuccess(File newFile) {
                        // 压缩成功后调用，返回压缩后的图片文件
                        // 获取返回的图片地址 newfile
                        RequestOptions requestOptions = new RequestOptions().centerCrop().placeholder(R.mipmap.icon_up_photo)
                                .error(R.mipmap.icon_up_photo).centerCrop();
                        switch (type) {
                            //个人照片
                            case "grzp":
                                grzpnewPath = newFile.getAbsolutePath();
                                Glide.with(AddKfxlApplyFileActivity.this).load(grzpnewPath).apply(requestOptions)
                                        .into(mIvGrzp);
                                break;
                            //监护人身份证
                            case "jhrsfz":
                                jhrsfznewPath = newFile.getAbsolutePath();
                                Glide.with(AddKfxlApplyFileActivity.this).load(jhrsfznewPath).apply(requestOptions)
                                        .into(mIvJhrsfz);
                                break;
                            //监护人户口簿
                            case "jhrhkb":
                                jhrhkbnewPath = newFile.getAbsolutePath();
                                Glide.with(AddKfxlApplyFileActivity.this).load(jhrhkbnewPath).apply(requestOptions)
                                        .into(mIvJhrhkb);
                                break;
                            //儿童户口簿
                            case "ethkb":
                                ethkbnewPath = newFile.getAbsolutePath();
                                Glide.with(AddKfxlApplyFileActivity.this).load(ethkbnewPath).apply(requestOptions)
                                        .into(mIvEthkb);
                                break;
                            //低保证或贫困证明
                            case "dbzhpkzm":
                                dbzhpkzmnewPath = newFile.getAbsolutePath();
                                Glide.with(AddKfxlApplyFileActivity.this).load(dbzhpkzmnewPath).apply(requestOptions)
                                        .into(mIvDbzhpkzm);
                                break;
                            //残疾证
                            case "cjz":
                                cjznewPath = newFile.getAbsolutePath();
                                Glide.with(AddKfxlApplyFileActivity.this).load(cjznewPath).apply(requestOptions)
                                        .into(mIvCjz);
                                break;
                            //医院证明
                            case "yyzm":
                                yyzmnewPath = newFile.getAbsolutePath();
                                Glide.with(AddKfxlApplyFileActivity.this).load(yyzmnewPath).apply(requestOptions)
                                    .into(mIvYyzm);
                                break;
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                    }
                }).launch(); // 启动压缩

    }
}
